package com.itheima.controller;

import com.itheima.anno.LogOperation;
import com.itheima.pojo.Clazz;
import com.itheima.pojo.ClazzQueryParam;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Result;
import com.itheima.service.ClazzService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/clazzs")
@Slf4j
public class ClazzController {

    @Autowired
    private ClazzService clazzService;


    /**
     * 修改班级
     * @param clazz
     * @return
     */
    @LogOperation
    @PutMapping
    public Result updateClazz(@RequestBody Clazz clazz){
        log.info("修改班级：{}",clazz);
        clazzService.updateClazz(clazz);
        return Result.success();
    }


    /**
     * 根据ID查询班级
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result selectById(@PathVariable Integer id){
        log.info("根据ID查询班级，id：{}", id);
        Clazz clazz = clazzService.selectById(id);
        return Result.success(clazz);
    }


    /**
     * 根据ID删除班级
     * @param id
     * @return
     */
    @LogOperation
    @DeleteMapping("/{id}")
    public Result deleteClazz(@PathVariable Integer id){
        log.info("根据ID删除班级，id：{}",id);
        clazzService.deleteById(id);
        return Result.success();
    }

    /**
     * 班级列表查询 （分页查询）
     * @param clazzQueryParam
     * @return
     */
    @GetMapping
    public Result page(ClazzQueryParam clazzQueryParam){
        log.info("查询班级列表：{}",clazzQueryParam);
        PageResult pageResult = clazzService.page(clazzQueryParam);
        return Result.success(pageResult);
    }

    /**
     * 查询所有班级
     * @return
     */
    @GetMapping("/list")
    public Result findAll(){
        return Result.success(clazzService.findAll());
    }


    /**
     * 添加班级
     * @param clazz
     * @return
     */
    @LogOperation
    @PostMapping
    public Result addClazz(@RequestBody Clazz clazz){
        clazzService.addClazz(clazz);
        return Result.success();
    }
}
